home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / pdc / libsrc / stdlib / strtoul.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  2KB  |  63 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* strtoul.c - converts ascii to an unsigned long */
  18.  
  19. #include <stddef.h>
  20. #include <ctype.h>
  21.  
  22. long strtoul(string, ptr, base)
  23. char *string;
  24. char **ptr;
  25. int base;
  26. {
  27.     char *strptr = string;
  28.     int retval = 0;
  29.     char c;
  30.     int i = 0;
  31.  
  32.     while (isspace(c = *strptr))
  33.         strptr++;
  34.  
  35.     if (base == 0) {
  36.         if (c == '0') {
  37.             if ((c = tolower(*(++strptr))) == 'x')
  38.                 base = 16;
  39.             else
  40.                 base = 8;
  41.             }
  42.         else
  43.             base = 10;
  44.         }
  45.  
  46.     do {
  47.         if (isalnum(c))
  48.             retval = (retval*base) + toint(c);
  49.         else
  50.             break;
  51.         i++;
  52.         } while ((c = *(++strptr)) != 0);
  53.  
  54.     if (ptr != NULL) {
  55.         if (i > 0)
  56.             *ptr = strptr;
  57.         else
  58.             *ptr = string;
  59.         }
  60.  
  61.     return(retval);
  62. }
  63.